#include "swagme.h"
#include "Character.h"
#include "Sprite.h"

const int Character::FrameCount = 3;
const int Character::speed = 3;

Character::Character(Sprite* ps, int x, int y)
  : Facing(sm_dir_right), prevFacing(sm_dir_right), FrameCounter(0) {
  Init(ps,x,y);
}

Character::Character(Sprite* ps)
  : Facing(sm_dir_right), prevFacing(sm_dir_right), FrameCounter(0) {
  Init(ps,0,0);
}

Character::Character()
  : Facing(sm_dir_right), prevFacing(sm_dir_right), FrameCounter(0) {
  Init(NULL,0,0);
}

void Character::Init(Sprite* ps, int x, int y)  {
  mpSprite = ps;
  posX=prevX=x;
  posY=prevY=y;
}

int Character::Draw(SDL_Surface *dst, int animSpeed, bool bShadow) {
  int r, frameNum ;

  if ( (posX != prevX) || (posY != prevY)) {
    if (++FrameCounter > FrameCount * 4) FrameCounter = 0;
    frameNum = (FrameCounter /4) % FRAMES_PER_ANIM;
  } else {
    FrameCounter = 1;
    frameNum = 1;
  }
  return mpSprite->Draw(Facing -1, frameNum,dst, posX+MAP_POS_X, posY+MAP_POS_Y, bShadow);
}

// Facing has a setter and getter method so facing changes can be animated if need be
void Character::SetFacing(sm_direction f) {
  prevFacing = Facing;
  Facing = f;
}
sm_direction Character::GetFacing() {
  return Facing;
}

Character::~Character() {
}
